home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / misc_lib / getarg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  24.6 KB  |  662 lines

  1. /***************************************************************************
  2. *  Routines to grab the    parameters from    the command line :           *
  3. * All the routines except the main one,    starts with GA (Get Arguments) to  *
  4. * prevent from names conflicts.                           *
  5. * It is    assumed    in these routine that any pointer, for any type    has the       *
  6. * same length (i.e. length of int pointer is equal to char pointer etc.)   *
  7. *                                       *
  8. *  The following routines are available    in this    module:               *
  9. * 1. int GAGetArgs(argc, argv, CtrlStr, Variables...)               *
  10. *    where argc, argv as received on entry.                   *
  11. *       CtrlStr is the contrl string    (see below)               *
  12. *       Variables are all the variables to be set according to CtrlStr. *
  13. *       Note    that all the variables MUST be transfered by address.       *
  14. *    return 0 on correct parsing, otherwise error number (see GetArg.h).   *
  15. * 2. GAPrintHowTo(CtrlStr)                           *
  16. *    Print the control string to stderr, in the    correct    format needed.       *
  17. *    This feature is very useful in case of error during GetArgs parsing.  *
  18. *    Chars equal to SPACE_CHAR are not printed (regular spaces are NOT     *
  19. *    allowed, and so using SPACE_CHAR you can create space in PrintHowTo). *
  20. * 3. GAPrintErrMsg(Error)                           *
  21. *    Print the error to    stderr,    according to Error (usually returned by       *
  22. *    GAGetArgs).                               *
  23. *                                       *
  24. *     CtrlStr format:                               *
  25. *   The    control    string passed to GetArgs controls the way argv (argc) are  *
  26. * parsed. Each entry in    this string must not have any spaces in    it. The       *
  27. * First    Entry is the name of the program which is usually ignored except   *
  28. * when GAPrintHowTo is called. All the other entries (except the last one  *
  29. * which    we will    come back to it    later) must have the following format:       *
  30. * 1. One letter    which sets the option letter.                   *
  31. * 2. '!' or '%'    to determines if this option is    really optional    ('%') or   *
  32. *    it    must exists ('!')...                           *
  33. * 3. '-' allways.                               *
  34. * 4. Alpha numeric string, usually ignored, but    used by    GAPrintHowTo to       *
  35. *    print the meaning of this input.                       *
  36. * 5. Sequences starts with '!' or '%'. Again if    '!' then this sequence       *
  37. *    must exists (only if its option flag is given of course), and if '%'  *
  38. *    it    is optional. Each sequence will    continue with one or two       *
  39. *    characters    which defines the kind of the input:               *
  40. *    a.    d, x, o, u - integer is expected (decimal, hex, octal base or       *
  41. *          unsigned).                           *
  42. *    b.    D, X, O, U - long integer is expected (same as above).           *
  43. *    c.    f    - float    number is expected.                   *
  44. *    d.    F    - double number    is expected.                   *
  45. *    e.    s    - string is expected.                       *
  46. *    f.    *?    - any number of    '?' kind (d, x, o, u, D, X, O, U, f, F, s) *
  47. *          will match this one. If '?' is numeric, it scans until   *
  48. *          none numeric input is given. If '?' is 's' then it scans *
  49. *          up to the next option or end of argv.               *
  50. *                                       *
  51. *   If the last    parameter given    in the CtrlStr,    is not an option (i.e. the *
  52. * second char is not in    ['!', '%'] and the third one is not '-'), all what *
  53. * remained from    argv is    linked to it.                       *
  54. *                                       *
  55. *   The    variables passed to GAGetArgs (starting    from 4th parameter) MUST   *
  56. * match    the order of the CtrlStr:                       *
  57. *   For    each option, one integer address must be passed. This integer must *
  58. * initialized by 0. If that option is given in the command line, it will   *
  59. * be set to one.                               *
  60. *   In addition, the sequences that might follow an option require the       *
  61. * following parameters to pass:                           *
  62. * 1. d, x, o, u - pointer to integer (int *).                   *
  63. * 2. D, X, O, U - pointer to long (long *).                   *
  64. * 3. f         - pointer to float      (float *).                   *
  65. * 4. F         - pointer to double  (double *).                   *
  66. * 5. s         - pointer to char      (char    *). NO allocation is needed!       *
  67. * 6. *?         - TWO variables are passed    for each wild request. the first   *
  68. *           one is (address of) integer, and    it will    return number of   *
  69. *           parameters actually matched this    sequence, and the second   *
  70. *           one is a    pointer    to pointer to ?    (? **),    and will return    an *
  71. *           address to a block of pointers to ? kind, terminated with   *
  72. *           NULL pointer. NO    pre-allocation is needed.           *
  73. *           note that these two variables are pretty    like the argv/argc *
  74. *           pair...                               *
  75. *                                       *
  76. *   Examples:                                   *
  77. *                                       *
  78. *    "Example1  i%-OneInteger!d  s%-Strings!*s  j%-  k!-Float!f  Files"       *
  79. * Will match: Example1 -i 77 -s    String1    String2    String3    -k 88.2    File1 File2*
  80. *   or match: Example1 -s String1 -k 88.3 -i 999 -j               *
  81. *    but not: Example1 -i 77 78    (option    i expects one integer, k must be). *
  82. * Note the option k must exists, and that the order of the options is not  *
  83. * not important. In the    first examples File1 & File2 will match    the Files  *
  84. * in the command line.                               *
  85. * A call to GAPrintHowTo with this CtrlStr will    print to stderr:       *
  86. * Example1 [-i OneIngeter] [-s Strings...] [-j]    -k Float Files...       *
  87. *                                       *
  88. *   Notes:                                   *
  89. *                                       *
  90. * 1. This module assumes that all the pointers to all kind of data types   *
  91. *    have the same length and format, i.e. sizeof(int *) == sizeof(char    *).*
  92. *                                       *
  93. *                      Gershon Elber    Ver 0.2     Mar 88       *
  94. ****************************************************************************
  95. * History:                                   *
  96. * 11 Mar 88 - Version 1.0 by Gershon Elber.                   *
  97. ***************************************************************************/
  98.  
  99. #ifdef USE_VARARGS
  100. #include <varargs.h>
  101. #else
  102. #include <stdarg.h>
  103. #endif /* USE_VARARGS */
  104.  
  105. #include <stdio.h>
  106. #include <ctype.h>
  107. #include <string.h>
  108. #include "irit_sm.h"
  109. #include "getarg.h"
  110. #include "imalloc.h"
  111.  
  112. #define    MAX_PARAM    100        /* maximum number of parameters allowed. */
  113. #define    CTRL_STR_MAX_LEN    1024
  114.  
  115. #define SPACE_CHAR    '|'      /* The character not to print using HowTo. */
  116.  
  117. #ifndef    TRUE
  118. #define    TRUE -1
  119. #define    FALSE 0
  120. #endif /* TRUE */
  121.  
  122. #define    ARG_OK    0
  123.  
  124. #define    ISSPACE(x) ((x)    <= ' ')           /* Not conventional - but works fine! */
  125. /* The two characters '%' and '!' are used in the control string: */
  126. #define    ISCTRLCHAR(x) (((x) == '%') || ((x) == '!'))
  127.  
  128. static char *GAErrorToken;/* On error code, ErrorToken is set to point on it.*/
  129.  
  130. static int GATestAllSatis(char *CtrlStrCopy, char *CtrlStr, int *argc,
  131.     char ***argv, int *Parameters[MAX_PARAM], int *ParamCount);
  132. static int GAUpdateParameters(int *Parameters[], int *ParamCount,
  133.     char *Option, char *CtrlStrCopy, char *CtrlStr, int *argc,
  134.     char ***argv);
  135. static int GAGetParmeters(int *Parameters[], int *ParamCount,
  136.     char *CtrlStrCopy , char *Option, int *argc, char ***argv);
  137. static int GAGetMultiParmeters(int *Parameters[], int *ParamCount,
  138.     char *CtrlStrCopy, int *argc, char ***argv);
  139. static void GASetParamCount(char *CtrlStr, int Max, int *ParamCount);
  140. static void GAByteCopy(char *Dst, char *Src, unsigned n);
  141. static int GAOptionExists(int argc, char **argv);
  142.  
  143. /***************************************************************************
  144. * Routine to access the    command    line argument and interpret them:       *
  145. * Return ARG_OK (0) is case of succesfull parsing, error code else...       *
  146. ***************************************************************************/
  147. #ifdef USE_VARARGS
  148. int GAGetArgs(int va_alist, ...)
  149. {
  150.     va_list ap;
  151.     int argc, i, Error = FALSE,
  152.     *Parameters[MAX_PARAM],           /* Save here parameter addresses. */
  153.     ParamCount = 0;
  154.     char **argv, *CtrlStr, *Option, CtrlStrCopy[CTRL_STR_MAX_LEN];
  155.  
  156.     va_start(ap);
  157.  
  158.     argc = va_arg(ap, int);
  159.     argv = va_arg(ap, char **);
  160.     CtrlStr = va_arg(ap, char *);
  161.  
  162.     strcpy(CtrlStrCopy, CtrlStr);
  163.  
  164.     /* Using base address of parameters we access other parameters addr:  */
  165.     /* Note that we (for sure!) samples data beyond the current function  */
  166.     /* frame, but we access only these set address only by demand.      */
  167.     for (i = 1; i <= MAX_PARAM; i++)
  168.     Parameters[i-1] = va_arg(ap, int *);
  169.  
  170.     va_end(ap);
  171. #else /* Using stdarg.h */
  172. int GAGetArgs(int argc, ...)
  173. {
  174.     va_list ap;
  175.     int i, Error = FALSE, ParamCount = 0,
  176.     *Parameters[MAX_PARAM];           /* Save here parameter addresses. */
  177.     char **argv, *CtrlStr, *Option, CtrlStrCopy[CTRL_STR_MAX_LEN];
  178.  
  179.     va_start(ap, argc);
  180.  
  181.     argv = va_arg(ap, char **);
  182.     CtrlStr = va_arg(ap, char *);
  183.  
  184.     strcpy(CtrlStrCopy, CtrlStr);
  185.  
  186.     /* Using base address of parameters we access other parameters addr:  */
  187.     /* Note that we (for sure!) samples data beyond the current function  */
  188.     /* frame, but we access only these set address only by demand.      */
  189.     for (i = 1; i <= MAX_PARAM; i++)
  190.     Parameters[i-1] = va_arg(ap, int *);
  191.  
  192.     va_end(ap);
  193. #endif /* USE_VARARG */
  194.  
  195.     --argc; argv++;        /* Skip the program name (first in argv/c list). */
  196.     while (argc >= 0) {
  197.     if (!GAOptionExists(argc, argv))
  198.         break;                        /* The loop. */
  199.     argc--;
  200.     Option    = *argv++;
  201.     if ((Error = GAUpdateParameters(Parameters, &ParamCount, Option,
  202.          CtrlStrCopy, CtrlStr, &argc, &argv)) != FALSE)
  203.         return Error;
  204.     }
  205.     /*    Check for results and update trail of command line: */
  206.     return GATestAllSatis(CtrlStrCopy, CtrlStr, &argc, &argv, Parameters,
  207.                                  &ParamCount);
  208. }
  209.  
  210. /***************************************************************************
  211. * Routine to search for    unsatisfied flags - simply scan    the list for !-       *
  212. * sequence. Before this    scan, this routine updates the rest of the command *
  213. * line into the    last two parameters if it is requested by the CtrlStr       *
  214. * (last    item in    CtrlStr    is NOT an option).                   *
  215. * Return ARG_OK if all satisfied, CMD_ERR_AllSatis error else.           *
  216. ***************************************************************************/
  217. static int GATestAllSatis(char *CtrlStrCopy, char *CtrlStr, int *argc,
  218.     char ***argv, int *Parameters[MAX_PARAM], int *ParamCount)
  219. {
  220.     int    i;
  221.     static char
  222.     *LocalToken = NULL;
  223.  
  224.     /* If LocalToken is not initialized - do it now. Note that this string */
  225.     /* should be writable as well so we can not assign it directly.        */
  226.     if (LocalToken == NULL) {
  227.         LocalToken = (char *) IritMalloc(3);
  228.     strcpy(LocalToken, "-?");
  229.     }
  230.  
  231.     /* Check is    last item is an    option.    If not then copy rest of command */
  232.     /* line into it as 1. NumOfprm, 2. pointer to block    of pointers.     */
  233.     for (i = (int) strlen(CtrlStr) - 1; i > 0 && !ISSPACE(CtrlStr[i]); i--);
  234.     if (!ISCTRLCHAR(CtrlStr[i + 2])) {
  235.     GASetParamCount(CtrlStr, i, ParamCount);   /* Point in correct prm.. */
  236.     *Parameters[(*ParamCount)++] = *argc;
  237.     GAByteCopy((char *) Parameters[(*ParamCount)++], (char *) argv,
  238.                             sizeof(char *));
  239.     }
  240.  
  241.     i =    0;
  242.     while (++i < (int) strlen(CtrlStrCopy))
  243.     if ((CtrlStrCopy[i] == '-') && (CtrlStrCopy[i-1] == '!')) {
  244.         GAErrorToken = LocalToken;
  245.         LocalToken[1] = CtrlStrCopy[i-2];        /* Set the corrent flag. */
  246.         return CMD_ERR_AllSatis;
  247.     }
  248.  
  249.     return ARG_OK;
  250. }
  251.  
  252. /***************************************************************************
  253. * Routine to update the    parameters according to    the given Option:       *
  254. ***************************************************************************/
  255. static int GAUpdateParameters(int *Parameters[], int *ParamCount,
  256.        char *Option, char *CtrlStrCopy, char *CtrlStr, int *argc, char ***argv)
  257. {
  258.     int    i,
  259.     BooleanTrue = Option[2] != '-';
  260.  
  261.     if (Option[0] != '-') {
  262.     GAErrorToken = Option;
  263.     return CMD_ERR_NotAnOpt;
  264.     }
  265.     i =    0;                /* Scan the CtrlStrCopy for that option: */
  266.     while (i + 2 < (int) strlen(CtrlStrCopy)) {
  267.     if ((CtrlStrCopy[i] == Option[1]) && (ISCTRLCHAR(CtrlStrCopy[i + 1]))
  268.         && (CtrlStrCopy[i+2] == '-')) {
  269.         /* We found    that option! */
  270.         break;
  271.     }
  272.     i++;
  273.     }
  274.     if (i + 2 >= (int) strlen(CtrlStrCopy)) {
  275.     GAErrorToken = Option;
  276.     return CMD_ERR_NoSuchOpt;
  277.     }
  278.  
  279.     /* If we are here, then we found that option in CtrlStr - Strip it off:  */
  280.     CtrlStrCopy[i] = CtrlStrCopy[i + 1] = CtrlStrCopy[i + 2] = (char) ' ';
  281.     GASetParamCount(CtrlStr, i, ParamCount);/*Set it to point in correct prm.*/
  282.     i += 3;
  283.     /* Set Boolean flag for that option. */
  284.     *Parameters[(*ParamCount)++] = BooleanTrue;
  285.     if (ISSPACE(CtrlStrCopy[i]))
  286.     return ARG_OK;               /* Only a Boolean flag is needed. */
  287.  
  288.     /* Skip the    text between the Boolean option and data follows: */
  289.     while (!ISCTRLCHAR(CtrlStrCopy[i]))
  290.     i++;
  291.     /* Get the parameters and return the appropriete return code: */
  292.     return GAGetParmeters(Parameters, ParamCount, &CtrlStrCopy[i],
  293.                               Option, argc, argv);
  294. }
  295.  
  296. /***************************************************************************
  297. * Routine to get parameters according to the CtrlStr given from    argv/c :   *
  298. ***************************************************************************/
  299. static int GAGetParmeters(int *Parameters[], int *ParamCount,
  300.     char *CtrlStrCopy , char *Option, int *argc, char ***argv)
  301. {
  302.     int    ScanRes, IData,
  303.     i = 0;
  304.     long LData;
  305.     float FData;
  306.     double DData;
  307.  
  308.     if ( *argc == 0 ) {
  309.     GAErrorToken = Option;
  310.     return CMD_ERR_NumRead;
  311.     }
  312.  
  313.     while (!(ISSPACE(CtrlStrCopy[i]))) {
  314.     switch (CtrlStrCopy[i+1]) {
  315.         case 'd':                     /* Get signed integers. */
  316.         if ((ScanRes = sscanf(*((*argv)++), "%d", &IData)) == 1)
  317.             *((int *) Parameters[(*ParamCount)++]) = IData;
  318.         break;
  319.         case 'u':                   /* Get unsigned integers. */
  320.         if ((ScanRes = sscanf(*((*argv)++), "%u", &IData)) == 1)
  321.             *((int *) Parameters[(*ParamCount)++]) = IData;
  322.         break;
  323.         case 'x':                    /* Get hex integers. */
  324.         if ((ScanRes = sscanf(*((*argv)++), "%x", &IData)) == 1)
  325.             *((int *) Parameters[(*ParamCount)++]) = IData;
  326.         break;
  327.         case 'o':                      /* Get octal integers. */
  328.         if ((ScanRes = sscanf(*((*argv)++), "%o", &IData)) == 1)
  329.             *((int *) Parameters[(*ParamCount)++]) = IData;
  330.         break;
  331.         case 'D':                     /* Get signed integers. */
  332.         if ((ScanRes = sscanf(*((*argv)++), "%ld", &LData)) == 1)
  333.             *((int *) Parameters[(*ParamCount)++]) = LData;
  334.         break;
  335.         case 'U':                   /* Get unsigned integers. */
  336.         if ((ScanRes = sscanf(*((*argv)++), "%lu", &LData)) == 1)
  337.             *((int *) Parameters[(*ParamCount)++]) = LData;
  338.         break;
  339.         case 'X':                    /* Get hex integers. */
  340.         if ((ScanRes = sscanf(*((*argv)++), "%lx", &LData)) == 1)
  341.             *((int *) Parameters[(*ParamCount)++]) = LData;
  342.         break;
  343.         case 'O':                      /* Get octal integers. */
  344.         if ((ScanRes = sscanf(*((*argv)++), "%lo", &LData)) == 1)
  345.             *((int *) Parameters[(*ParamCount)++]) = LData;
  346.         break;
  347.         case 'f':                    /* Get float number. */
  348.         if ((ScanRes = sscanf(*((*argv)++), "%f", &FData)) == 1)
  349.             *((float *) Parameters[(*ParamCount)++]) = FData;
  350.         break;
  351.         case 'F':                 /* Get double float number. */
  352.         if ((ScanRes = sscanf(*((*argv)++), "%lf", &DData)) == 1)
  353.             *((double *) Parameters[(*ParamCount)++]) = DData;
  354.         break;
  355.         case 's':                      /* It as a string. */
  356.         ScanRes    = 1;                     /* Allways O.K. */
  357.         GAByteCopy((char *) Parameters[(*ParamCount)++],
  358.                     (char *) ((*argv)++), sizeof(char *));
  359.         break;
  360.         case '*':                 /* Get few parameters into one: */
  361.         ScanRes    = GAGetMultiParmeters(Parameters, ParamCount,
  362.                           &CtrlStrCopy[i], argc, argv);
  363.         if ((ScanRes ==    0) && (CtrlStrCopy[i] == '!')) {
  364.             GAErrorToken = Option;
  365.             return CMD_ERR_WildEmpty;
  366.         }
  367.         break;
  368.         default:
  369.         ScanRes = 0;               /* Make optimizer warning silent. */
  370.     }
  371.     /* If reading fails and    this number is a must (!) then error: */
  372.     if (ScanRes == 0) {
  373.         if (CtrlStrCopy[i] == '!') {
  374.         GAErrorToken = Option;
  375.         return CMD_ERR_NumRead;
  376.         }
  377.         else {
  378.         /* It is optional but it is not there - quit. */
  379.         (*argv)--;
  380.         while (!isspace(CtrlStrCopy[i++]));
  381.         return ARG_OK;
  382.         }
  383.     }
  384.     if (CtrlStrCopy[i+1] !=    '*') {
  385.         (*argc)--;         /* Everything is OK - update to next parameter: */
  386.         i += 2;             /* Skip to next parameter (if any). */
  387.     }
  388.     else
  389.         i += 3;                       /* Skip the '*' also! */
  390.     }
  391.  
  392.     return ARG_OK;
  393. }
  394.  
  395. /***************************************************************************
  396. * Routine to get few parameters    into one pointer such that the returned       *
  397. * pointer actually points on a block of    pointers to the    parameters...       *
  398. * For example *F means a pointer to pointers on    floats.               *
  399. * Returns number of parameters actually    read.                   *
  400. * This routine assumes that all    pointers (on any kind of scalar) has the   *
  401. * same size (and the union below is totally ovelapped bteween dif. arrays) *
  402. ***************************************************************************/
  403. static int GAGetMultiParmeters(int *Parameters[], int *ParamCount,
  404.     char *CtrlStrCopy, int *argc, char ***argv)
  405. {
  406.     int    ScanRes, **Pmain, **Ptemp,
  407.     i = 0,
  408.     NumOfPrm = 0;
  409.     union TmpArray {    /* Save here the temporary data before copying it to */
  410.     int    *IntArray[MAX_PARAM];          /* the returned pointer block. */
  411.     long   *LngArray[MAX_PARAM];
  412.     float  *FltArray[MAX_PARAM];
  413.     double *DblArray[MAX_PARAM];
  414.     char   *ChrArray[MAX_PARAM];
  415.     } TmpArray;
  416.  
  417.     do {
  418.     switch (CtrlStrCopy[2]) {   /* CtrlStr == '!*?' or '%*?' where ? is. */
  419.         case 'd':               /* Format to read the parameters: */
  420.         TmpArray.IntArray[NumOfPrm] =
  421.             (int *) IritMalloc(sizeof(int));
  422.         ScanRes    = sscanf(*((*argv)++), "%d",
  423.                        (int *) TmpArray.IntArray[NumOfPrm++]);
  424.         break;
  425.         case 'u':
  426.         TmpArray.IntArray[NumOfPrm] =
  427.             (int *) IritMalloc(sizeof(int));
  428.         ScanRes    = sscanf(*((*argv)++), "%u",
  429.                   (unsigned    int *) TmpArray.IntArray[NumOfPrm++]);
  430.         break;
  431.         case 'o':
  432.         TmpArray.IntArray[NumOfPrm] =
  433.             (int *) IritMalloc(sizeof(int));
  434.         ScanRes    = sscanf(*((*argv)++), "%o",
  435.                        (int *) TmpArray.IntArray[NumOfPrm++]);
  436.         break;
  437.         case 'x':
  438.         TmpArray.IntArray[NumOfPrm] =
  439.             (int *) IritMalloc(sizeof(int));
  440.         ScanRes    = sscanf(*((*argv)++), "%x",
  441.                        (int *) TmpArray.IntArray[NumOfPrm++]);
  442.         break;
  443.         case 'D':
  444.         TmpArray.LngArray[NumOfPrm] = 
  445.             (long *) IritMalloc(sizeof(long));
  446.         ScanRes    = sscanf(*((*argv)++), "%ld",
  447.                       (long *) TmpArray.LngArray[NumOfPrm++]);
  448.         break;
  449.         case 'U':
  450.         TmpArray.LngArray[NumOfPrm] = 
  451.             (long *) IritMalloc(sizeof(long));
  452.         ScanRes    = sscanf(*((*argv)++), "%lu",
  453.                  (unsigned long *) TmpArray.LngArray[NumOfPrm++]);
  454.         break;
  455.         case 'O':
  456.         TmpArray.LngArray[NumOfPrm] = 
  457.             (long *) IritMalloc(sizeof(long));
  458.         ScanRes    = sscanf(*((*argv)++), "%lo",
  459.                       (long *) TmpArray.LngArray[NumOfPrm++]);
  460.         break;
  461.         case 'X':
  462.         TmpArray.LngArray[NumOfPrm] = 
  463.             (long *) IritMalloc(sizeof(long));
  464.         ScanRes    = sscanf(*((*argv)++), "%lx",
  465.                       (long *) TmpArray.LngArray[NumOfPrm++]);
  466.         break;
  467.         case 'f':
  468.         TmpArray.FltArray[NumOfPrm] =
  469.             (float *) IritMalloc(sizeof(float));
  470.         ScanRes    = sscanf(*((*argv)++), "%f",
  471.                      (float *) TmpArray.FltArray[NumOfPrm++]);
  472.         break;
  473.         case 'F':
  474.         TmpArray.DblArray[NumOfPrm] =
  475.             (double *) IritMalloc(sizeof(double));
  476.         ScanRes    = sscanf(*((*argv)++), "%lf",
  477.                     (double *) TmpArray.DblArray[NumOfPrm++]);
  478.         break;
  479.         case 's':
  480.         while ((*argc) && ((**argv)[0] != '-'))    {
  481.             TmpArray.ChrArray[NumOfPrm++] = *((*argv)++);
  482.             (*argc)--;
  483.         }
  484.         ScanRes    = 0;               /* Force quit from do - loop. */
  485.         NumOfPrm++;        /* Updated again immediately after loop! */
  486.         (*argv)++;                           /* "" */
  487.         break;
  488.         default:
  489.         ScanRes = 0;               /* Make optimizer warning silent. */
  490.     }
  491.     (*argc)--;
  492.     }
  493.     while (ScanRes == 1);          /* Exactly one parameter was read. */
  494.     (*argv)--; NumOfPrm--; (*argc)++;
  495.  
  496.     /* Now allocate the    block with the exact size, and set it: */
  497.     Ptemp = Pmain = (int **) IritMalloc((unsigned) (NumOfPrm+1) * sizeof(int *));
  498.     /* And here    we use the assumption that all pointers    are the    same: */
  499.     for (i = 0; i < NumOfPrm; i++)
  500.     *Ptemp++ = TmpArray.IntArray[i];
  501.     *Ptemp = NULL;               /* Close the block with NULL pointer. */
  502.  
  503.     /* That it save the    number of parameters read as first parameter to    */
  504.     /* return and the pointer to the block as second, and return:    */
  505.     *Parameters[(*ParamCount)++] = NumOfPrm;
  506.     GAByteCopy((char *)    Parameters[(*ParamCount)++], (char *) &Pmain,
  507.                                  sizeof(char *));
  508.     return NumOfPrm;
  509. }
  510.  
  511. /***************************************************************************
  512. * Routine to scan the CtrlStr, upto Max    and count the number of    parameters *
  513. * to that point:                               *
  514. * 1. Each option is counted as one parameter - Boolean variable    (int)       *
  515. * 2. Within an option, each %? or !? is    counted    once - pointer to something*
  516. * 3. Within an option, %*? or !*? is counted twice - one for item count       *
  517. *    and one for pointer to block pointers.                   *
  518. * Note ALL variables are passed    by address and so of fixed size    (address). *
  519. ***************************************************************************/
  520. static void GASetParamCount(char *CtrlStr, int Max, int *ParamCount)
  521. {
  522.     int    i;
  523.  
  524.     *ParamCount    = 0;
  525.     for (i = 0; i < Max; i++)
  526.     if (ISCTRLCHAR(CtrlStr[i])) {
  527.         if (CtrlStr[i+1] == '*')
  528.         *ParamCount += 2;
  529.         else
  530.         (*ParamCount)++;
  531.     }
  532. }
  533.  
  534. /***************************************************************************
  535. * Routine to copy exactly n bytes from Src to Dst. Note system library       *
  536. * routine strncpy should do the    same, but it stops on NULL char    !       *
  537. ***************************************************************************/
  538. static void GAByteCopy(char *Dst, char *Src, unsigned n)
  539. {
  540.     while (n--)    *(Dst++) = *(Src++);
  541. }
  542.  
  543. /***************************************************************************
  544. * Routine to check if more option (i.e.    first char == '-') exists in the   *
  545. * given    list argc, argv (excludes just - which signals stdin, though):       *
  546. ***************************************************************************/
  547. static int GAOptionExists(int argc, char **argv)
  548. {
  549.     while (argc--) {
  550.     if ((*argv)[0] == '-' && (*argv)[1] != 0)
  551.         return TRUE;
  552.     argv++;
  553.     }
  554.     return FALSE;
  555. }
  556.  
  557. /***************************************************************************
  558. * Routine to print some    error messages,    for this module:           *
  559. ***************************************************************************/
  560. void GAPrintErrMsg(int Error)
  561. {
  562.     fprintf(stderr, "Error in command line parsing - ");
  563.     switch (Error) {
  564.     case 0:;
  565.         fprintf(stderr, "Undefined error");
  566.         break;
  567.     case CMD_ERR_NotAnOpt:
  568.         fprintf(stderr, "None option found");
  569.         break;
  570.     case CMD_ERR_NoSuchOpt:
  571.         fprintf(stderr, "Undefined option found");
  572.         break;
  573.     case CMD_ERR_WildEmpty:
  574.         fprintf(stderr, "Empty input for '!*?' seq.");
  575.         break;
  576.     case CMD_ERR_NumRead:
  577.         fprintf(stderr, "Failed on reading number");
  578.         break;
  579.     case CMD_ERR_AllSatis:
  580.         fprintf(stderr, "Fail to satisfy");
  581.         break;
  582.     }
  583.     fprintf(stderr, " - '%s'.\n", GAErrorToken);
  584. }
  585.  
  586. /***************************************************************************
  587. * Routine to print correct format of command line allowed:           *
  588. ***************************************************************************/
  589. void GAPrintHowTo(char *CtrlStr)
  590. {
  591.     int    SpaceFlag,
  592.     i = 0;
  593.  
  594.     fprintf(stderr, "Usage: ");
  595.     /* Print program name - first word in ctrl.    str. (optional): */
  596.     while (!(ISSPACE(CtrlStr[i])) && (!ISCTRLCHAR(CtrlStr[i+1])))
  597.     fprintf(stderr, "%c", CtrlStr[i++]);
  598.  
  599.     while (i < (int) strlen(CtrlStr))    {
  600.     while ((ISSPACE(CtrlStr[i])) &&    (i < (int) strlen(CtrlStr))) i++;
  601.     switch (CtrlStr[i+1]) {
  602.         case '%':
  603.         fprintf(stderr, " [-%c", CtrlStr[i++]);
  604.         i += 2;             /* Skip the '%-' or '!- after the char! */
  605.         SpaceFlag = TRUE;
  606.         while (!ISCTRLCHAR(CtrlStr[i]) &&
  607.                (i < (int) strlen(CtrlStr)) &&
  608.                (!ISSPACE(CtrlStr[i])))
  609.             if (SpaceFlag) {
  610.             if (CtrlStr[i++] == SPACE_CHAR)
  611.                 fprintf(stderr, " ");
  612.             else
  613.                 fprintf(stderr, " %c", CtrlStr[i-1]);
  614.             SpaceFlag = FALSE;
  615.             }
  616.             else if (CtrlStr[i++] == SPACE_CHAR)
  617.             fprintf(stderr, " ");
  618.             else
  619.             fprintf(stderr, "%c", CtrlStr[i-1]);
  620.         while (!ISSPACE(CtrlStr[i]) && (i < (int) strlen(CtrlStr))) {
  621.             if (CtrlStr[i] == '*')
  622.             fprintf(stderr, "...");
  623.             i++;                 /* Skip the rest of it. */
  624.         }
  625.         fprintf(stderr, "]");
  626.         break;
  627.         case '!':
  628.         fprintf(stderr, " -%c", CtrlStr[i++]);
  629.         i += 2;             /* Skip the '%-' or '!- after the char! */
  630.         SpaceFlag = TRUE;
  631.         while (!ISCTRLCHAR(CtrlStr[i]) &&
  632.                (i < (int) strlen(CtrlStr)) &&
  633.                (!ISSPACE(CtrlStr[i])))
  634.             if (SpaceFlag) {
  635.             if (CtrlStr[i++] == SPACE_CHAR)
  636.                 fprintf(stderr, " ");
  637.             else
  638.                 fprintf(stderr, " %c", CtrlStr[i-1]);
  639.             SpaceFlag = FALSE;
  640.             }
  641.             else if (CtrlStr[i++] == SPACE_CHAR)
  642.             fprintf(stderr, " ");
  643.             else
  644.                 fprintf(stderr, "%c", CtrlStr[i-1]);
  645.         while (!ISSPACE(CtrlStr[i]) && (i < (int) strlen(CtrlStr))) {
  646.             if (CtrlStr[i] == '*')
  647.             fprintf(stderr, "...");
  648.             i++;                 /* Skip the rest of it. */
  649.         }
  650.         break;
  651.         default:               /* Not checked, but must be last one! */
  652.         fprintf(stderr, " ");
  653.         while (!ISSPACE(CtrlStr[i]) && (i < (int) strlen(CtrlStr)) &&
  654.                !ISCTRLCHAR(CtrlStr[i]))
  655.             fprintf(stderr, "%c", CtrlStr[i++]);
  656.         fprintf(stderr, "\n");
  657.         return;
  658.     }
  659.     }
  660.     fprintf(stderr, "\n");
  661. }
  662.